package com.yanqu.road.server.cmd.rank;

import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.sync.message.SyncResponse;
import com.yanqu.road.entity.enums.eBigRankType;
import com.yanqu.road.logic.rank.RankListModel;
import com.yanqu.road.logic.rank.UnionRankListModel;
import com.yanqu.road.entity.enums.eRankType;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.syncMessage.SyncUnion;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.rank.RankMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;

import java.util.concurrent.CompletableFuture;

@Cmd(code = Protocol.S_RANK_TOP, desc = "获取榜首数据")
public class GetRankTopCmd extends AbstractCommand {

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

	}

	@Override
	public void sendData(GamePlayer player) {
		RankProto.RankTopOneRespMsg.Builder respMsg = RankProto.RankTopOneRespMsg.newBuilder();
		//赚速榜首
		RankListModel rankListModel = RankMgr.getRankListModelByType(eBigRankType.Normal.getValue(), eRankType.EarnSpeed.getValue());
		RankProto.RankListMsg.Builder rankListMsg;
		if(null != rankListModel){
			rankListMsg = rankListModel.getRankListMsg();
			if(rankListMsg.getRankListCount() > 0) {
				respMsg.setEarnSpeedRankTop(rankListMsg.getRankList(0));
			}
		}
		//实力榜首
		rankListModel = RankMgr.getRankListModelByType(eBigRankType.Normal.getValue(), eRankType.PatronsAbility.getValue());
		if(null != rankListModel){
			rankListMsg = rankListModel.getRankListMsg();
			if(rankListMsg.getRankListCount() > 0){
				respMsg.setAbilityRankTop(rankListMsg.getRankList(0));
			}
		}
		//亲密度榜首
		rankListModel = RankMgr.getRankListModelByType(eBigRankType.Normal.getValue(), eRankType.BeautyIntimacy.getValue());
		if(null != rankListModel){
			rankListMsg = rankListModel.getRankListMsg();
			if(rankListMsg.getRankListCount() > 0){
				respMsg.setIntimacyRankTop(rankListMsg.getRankList(0));
			}
		}

		//商会榜首
		if(CrossUnionMgr.crossGroupOpen()) {
			SyncUnion.UnionRankTopResponse.Builder unionRankTopResponse = SyncUnion.UnionRankTopResponse.newBuilder();
			CompletableFuture<SyncResponse> responseFuture = GamePlayerMgr.sendToCrossServer(player.getUserId(), CrossProtocol.C_UNION_RANK_TOP, null, unionRankTopResponse);
			responseFuture.thenAccept( response -> {
				if (response.getErrorCode() == 0) {
					RankProto.UnionRankTempMsg.Builder builder = RankProto.UnionRankTempMsg.newBuilder();
					builder.setUnionUid(unionRankTopResponse.getUnionUid());
					builder.setUnionBaseData(unionRankTopResponse.getUnionBaseData());
					respMsg.setUnionRankTop(builder);
					player.sendPacket(Protocol.U_RANK_TOP, respMsg);
				}
			});
		}else {
			UnionRankListModel unionRankListModel = RankMgr.getUnionRankList();
			if (null != unionRankListModel) {
				if (unionRankListModel.getRankListMsg().getRankListCount() > 0) {
					respMsg.setUnionRankTop(unionRankListModel.getRankListMsg().getRankList(0));
				}
			}
			player.sendPacket(Protocol.U_RANK_TOP, respMsg);
		}
	}
}
